package com.suos.utils;

/**
 * KB、 MB、 GB、 TB转换工具
 *
 * @author : MisShi
 * @date: 2021-10-20 17:03:45
 */
public class ChangeSizeUtil {

    private final static long KB = 1024;
    private final static long MB = KB * 1024;
    private final static long GB = MB * 1024;
    private final static long TB = GB * 1024;

    /**
     * cpu jvm memory 转换
     *
     * @param size 大小
     * @return 转换后值
     */
    public static String jcmSize(long size) {
        if (size >= TB) {
            return String.format("%.2f T", (float) size / TB);
        } else if (size >= GB) {
            return String.format("%.2f G", (float) size / GB);
        } else if (size >= MB) {
            float f = (float) size / MB;
            return String.format(f > 100 ? "%.2f M" : "%.2f MB", f);
        } else if (size >= KB) {
            float f = (float) size / KB;
            return String.format(f > 100 ? "%.2f K" : "%.2f KB", f);
        } else {
            return String.format("%d B", size);
        }
    }

    /**
     * disk 转换
     *
     * @param size 字节大小
     * @return 转换后值
     */
    public static String diskSize(long size) {
        if (size >= TB) {
            return String.format("%.2f TB", (float) size / TB);
        } else if (size >= GB) {
            return String.format("%.2f GB", (float) size / GB);
        } else if (size >= MB) {
            float f = (float) size / MB;
            return String.format("%.2f MB", f);
        } else if (size >= KB) {
            float f = (float) size / KB;
            return String.format("%.2f KB", f);
        } else {
            return String.format("%d B", size);
        }
    }
}
